class RaidEvent < ActiveRecord::Base
  belongs_to :raid
  belongs_to :event
  has_many :witnesses, :dependent => :destroy
  has_many :item_drops #TODO: before destroy set the item_drop.raid_event to nil
  validates_presence_of :raid, :event
  #validates_associated :raid

  def total
    #TODO: There's a better way to do it I'm sure.
    #The following is better in a certain sense.
    #It only works on saved objects tho, since it's a sql statement.
    #Witness.sum(:value, :conditions => {:raid_event_id => self.id})

    total = 0.0
    witnesses.each do |w|
      total += w.value
    end
    
    return total
  end

  def generate_attendance()
    unless raid
      throw "No raid to attend: " + self.to_yaml
    end

    raid.characters.each do |character|
      witness = Witness.new
      witness.character = character
      witness.raid_event = self
      witness.value
      witnesses << witness unless witness.raw_value == 0.0
    end
  end

end
